package com.bygf.curve.utils;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;

/**
 * 鼠标悬浮格式化对象
 *
 * @author sunyupei
 */
public class ToolTipFormat {

    //整体格式化字符串，{0}-曲线名称，{1}-X轴数值，{2]-Y轴数值
    private String formatString = "{0}:{1},{2}";
    //X数值格式化对象
    private Format xFormat = new DecimalFormat("#.#");
    //Y数值格式化对象
    private Format yFormat = new DecimalFormat("#.#");

    /**
     * 构造方法
     */
    public ToolTipFormat() {
    }

    /**
     * 构造方法
     *
     * @param formatString
     * @param xFormat
     * @param yFormat
     */
    public ToolTipFormat(String formatString, Format xFormat, Format yFormat) {
        this.formatString = formatString == null ? this.formatString : formatString;
        this.xFormat = xFormat == null ? this.xFormat : xFormat;
        this.yFormat = yFormat == null ? this.yFormat : yFormat;
    }

    /**
     * 格式化方法，替换格式化字符串中的{0},{1},{2}
     *
     * @param list ，0-曲线名称，1-X轴数值，2-Y轴数值
     * @return
     */
    public String format(ArrayList list) {
        String name = list.get(0).toString();
        String xValue = xFormat.format(list.get(1));
        String yValue = yFormat.format(list.get(2));
        String toolTip = formatString.replace("{0}", name).replace("{1}", xValue).replace("{2}", yValue);
        return toolTip;
    }

    /**
     * @return the formatString
     */
    public String getFormatString() {
        return formatString;
    }

    /**
     * @param formatString the formatString to set
     */
    public void setFormatString(String formatString) {
        if (formatString == null) {
            return;
        }
        this.formatString = formatString;
    }

    /**
     * @return the xFormat
     */
    public Format getxFormat() {
        return xFormat;
    }

    /**
     * @param xFormat the xFormat to set
     */
    public void setxFormat(Format xFormat) {
        if (xFormat == null) {
            return;
        }
        this.xFormat = xFormat;
    }

    /**
     * @return the yFormat
     */
    public Format getyFormat() {
        return yFormat;
    }

    /**
     * @param yFormat the yFormat to set
     */
    public void setyFormat(Format yFormat) {
        if (yFormat == null) {
            return;
        }
        this.yFormat = yFormat;
    }
}
